namespace Domotica.Media
{
    using System;
    using System.IO;
    using System.Text;

    public class Album : IComparable<Album>
    {
        private static string _albumArtDirectory;
        private string _AlbumArtName;
        private Domotica.Media.Artist _artist;
        private string _name;
        private TrackList _tracks = new TrackList();

        public Album(string name, Domotica.Media.Artist artist)
        {
            this._name = name;
            this._artist = artist;
        }

        public int CompareTo(Album other)
        {
            return this.Name.CompareTo(other.Name);
        }

        private string GetAlbumArtName(AlbumArtSize size)
        {
            string dir = Path.GetDirectoryName(this.Tracks[0].Location);
            if (Directory.Exists(dir))
            {
                return this.GetRealAlbumArtFileName(dir, size);
            }
            return "";
        }

        private string GetAlphanumericString(string s)
        {
            StringBuilder sb = new StringBuilder();
            string t = s;
            int i = 0;
            int count = t.Length;
            while (i < count)
            {
                char c = t[i];
                if (char.IsLetterOrDigit(c))
                {
                    sb.Append(c);
                }
                i++;
            }
            return sb.ToString();
        }

        private string GetCustomAlbumArtFileName(AlbumArtSize size)
        {
            return string.Format("{0}-{1}-{2}.jpg", this.GetAlphanumericString(this._artist.Name), this.GetAlphanumericString(this.Name), size.ToString());
        }

        private string GetRealAlbumArtFileName(string dir, AlbumArtSize size)
        {
            string filename = "";
            string[] filenames = Directory.GetFiles(dir, "AlbumArt*" + size.ToString() + ".jpg");
            if (filenames.Length > 0)
            {
                return filenames[0];
            }
            if (size == AlbumArtSize.Large)
            {
                FileInfo file = new FileInfo(Path.Combine(dir, "Folder.jpg"));
                if (file.Exists)
                {
                    filename = file.FullName;
                }
            }
            return filename;
        }

        public override string ToString()
        {
            return this.Name;
        }

        public string AlbumArtName
        {
            get
            {
                if (string.IsNullOrEmpty(this._AlbumArtName))
                {
                    this._AlbumArtName = this.GetAlbumArtName(AlbumArtSize.Large);
                }
                return this._AlbumArtName;
            }
        }

        public Domotica.Media.Artist Artist
        {
            get
            {
                return this._artist;
            }
        }

        public string Name
        {
            get
            {
                return this._name;
            }
        }

        public TrackList Tracks
        {
            get
            {
                return this._tracks;
            }
        }
    }
}

